package cn.com.dstz.admin.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import cn.com.dstz.utils.DataBaseUtils;
import cn.com.dstz.utils.DateUtils;
import cn.com.dstz.utils.NumberUtils;
import cn.com.dstz.utils.RequestUtils;
import cn.com.dstz.utils.ValidUtils;
import cn.com.dstz.web.constants.Constants;
import cn.com.dstz.base.action.BaseAction;

/**
 * 权限设置Action
 * @author 叶装装
 *
 */
public class FunctionAct extends BaseAction{
	
	private static final long serialVersionUID = 1L;
	
	public void list( ){
		
		int isSearch = NumberUtils.getInt( getParameter( "isSearch" ), 0 );
		Map< String, Object > valueMap = new HashMap< String, Object > ( );
		valueMap.put( "parentid", getParentid( ) );
		
		String SQL = "select * from dstz_function where parentid=:parentid";
		
		if( isSearch == 1 ){
			SQL = "select * from dstz_function where 1=1";
			valueMap.clear( );
		}
		
		this.ajaxResponse( JSONObject.fromObject( this.paginationBySQL( SQL, null, valueMap,  "lastupdatetime" ) )  );
		return;
	}
	
	public void add( ){
		
		int validSaveOrUpdate = validSaveOrUpdate( 0 );
		
		if( validSaveOrUpdate > 0 ){
			this.ajaxResponse( validSaveOrUpdate ); // 返回错误消息
			return;
		}
		
		Map< String, Object > admin =this.getEntityInSession( Constants.ADMIN_IN_SESSION );
		
		String datetime = DateUtils.getCurrentTime( );
		
		int save = baseService.save( "dstz_function", DataBaseUtils.getInsertData( new String[]{
				"name", "url", "parentid", "isdisplay", "description", "addtime", "lastupdatetime", "adduser", "ip", "displayorder"
		}, new Object[]{
				name, url, parentid, isdisplay, description, datetime, datetime,  admin.get( "username" ), RequestUtils.getIpAddr( getRequest() ),
				displayorder
		} ) );
		
		this.addLog( save, "function", 0, name );
		
		this.ajaxResponse( save > 0 ? 0 : -1 ); // 
		
		return;
	}
	
	public void edit( ){
		
		int id = NumberUtils.getInt( getParameter( "id" ), 0 );
		
		int validSaveOrUpdate = validSaveOrUpdate( 1 );
		if( validSaveOrUpdate > 0 ){
			this.ajaxResponse( validSaveOrUpdate ); // 返回错误消息
			return;
		}
		
		Map< String, Object > admin =this.getEntityInSession( Constants.ADMIN_IN_SESSION );
		
		String datetime = DateUtils.getCurrentTime( );
		
		int update = baseService.update( "dstz_function", DataBaseUtils.getData( new String[]{
				"name", "url", "parentid", "isdisplay", "description",  "lastupdatetime", "adduser", "ip", "displayorder"
		}, new Object[]{
				name, url, parentid, isdisplay, description,  datetime,  admin.get( "username" ), RequestUtils.getIpAddr( getRequest() ), displayorder
		}  ), DataBaseUtils.getData( new String[]{"id"}, new Object[]{id} ) );
		
		
		this.addLog( update, "function", 1,  "ID：" + id);
		
		this.ajaxResponse( update > 0 ? 0 : -1 ); 
		
		return;
		
	}
	
	/**
	 * 执行删除操作
	 */
	public void delete( ){
		
		String id = getParameter( "ids" );
		int del = -1;
		
		if( StringUtils.isNotBlank( id )){
			
			String selectSql = "select count(*) as a from dstz_function where parentid=:parentid";
			
			int count = baseService.countBySQL( selectSql, DataBaseUtils.getData( new String[]{
					"parentid"
			}, new Object[]{id} ) );
			
			if( count > 0 ){
				this.ajaxResponse( 1 ); // 存在子类
				return ;
			}
			
			del = baseService.deleteBySql( "dstz_function", DataBaseUtils.getData( new String[]{
					"id"
			}, new Object[]{
					id
			} ) );
		}
		
		this.addLog( del, "function", 2, "ID: " + id );
		
		this.ajaxResponse( del > 0 ? 0 : -1 );
		return;
		
	}
	
	public String toadd( ){
		setAllList( );
		return "add";
	}
	
	public String toedit( ){
		setAllList( );
		int id = NumberUtils.getInt( getParameter( "id" ), 0 );
		if( id > 0 ){
			String sql = "select * from dstz_function where id=:id";
			Map< String, Object > valueMap = new HashMap< String, Object >( );
			valueMap.put( "id", id );
			Map< String, Object > data = baseService.queryForMap( sql, valueMap );
			getRequest( ).setAttribute( "data", data );
		}
		
		
		return "edit";
	}
	
	private void setAllList( ){
		String sql = "select name,id from dstz_function where parentid=:parentid";
		Map< String, Object> valueMap = new HashMap< String, Object >( );
		valueMap.put( "parentid", 0 );
		List< Map< String, Object >> list = baseService.queryForListByAll( sql, valueMap );
		getRequest( ).setAttribute( "list", list );
	}
	
	/**
	 * 保存或者更新之前的验证
	 * @param saveOrUpdate  0 为保存，1为更新
	 * @return
	 */
	private int validSaveOrUpdate( int saveOrUpdate ){
		if( ValidUtils.validLength( name, 1, 100 ) ){
			return 1;
		}
		if( this.parentid > 0 ){
			
			if(  ValidUtils.validLength( url, 1, 255 ) ){
				return 2;
			}
			
			if( !url.startsWith( "/" )){
				return 4;
			}
			
		}
		
		if( ValidUtils.validLengthCanEmpty( description, 255 )){
			return 3;
		}
		
		
		if( parentid == 0 ){
			return 0;
		}
		
		int count = 0;
		int type = 0;
		
		// 表示是更新的
		if( saveOrUpdate == 1 ){
			if(!StringUtils.equals( oldname, name )){
				type = 1; //  如果名称不相等， 则去查询名称的
			}else if( !StringUtils.equals( url, oldurl )){
				type = 2; // 如果URL不相等， 就去查询URL,
			}else if(  !StringUtils.equals( oldname, name ) && !StringUtils.equals( url, oldurl ) ){
				type = 3; // 如果修改了名称和URL, 或者是添加， 就去查询全部
			}
		}else if( saveOrUpdate == 0 ){
			type = 3;
		}
		
		count = dbValid( type );
		
		if( count > 0 ){
			return 5;
		}
		
		return 0;
	}
	
	private int dbValid( int type ){
		if( type == 0 ){
			return 0;
		}
		
		String sql = "select count(*) as a from dstz_function where ";
		Map< String, Object > valueMap = new HashMap< String, Object > ( );
		
		if( type == 1 ){
			sql += " name=:name and parentid!=:parentid ";
			valueMap.put( "name", name );
			
		}else if( type == 2 ){
			sql += " url =:url and parentid!=:parentid";
			valueMap.put( "url", url );
		}else{
			sql += " ( name=:name or url=:url ) and parentid!=:parentid";
			valueMap.put( "name", name );
			valueMap.put( "url", url );
		}
		
		valueMap.put( "parentid", 0 );
		
		return  baseService.countBySQL( sql, valueMap );
		
	}
	
	private String name;
	private String url;
	private String oldname;
	private String oldurl;
	private String description;
	private Integer isdisplay;
	private Integer displayorder;
	
	private Integer parentid;

	public String getName( ) {
		return name;
	}

	public void setName( String name ) {
		if( name != null ){
			name = StringUtils.trim( decode( name ) );
		}
		this.name = name;
	}

	public String getUrl( ) {
		return url;
	}

	public void setUrl( String url ) {
		if( StringUtils.isNotBlank( url )){
			url = StringUtils.trim( decode( url ) );
		}
		this.url = url;
	}

	public Integer getParentid( ) {
		if( parentid == null ){
			parentid = 0;
		}
		return parentid;
	}

	public void setParentid( Integer parentid ) {
		if( parentid == null ){
			parentid = 0;
		}
		this.parentid = parentid;
	}

	public String getOldname( ) {
		return oldname;
	}

	public void setOldname( String oldname ) {
		if( oldname != null ){
			oldname = decode( oldname );
		}
		this.oldname = oldname;
	}

	public String getOldurl( ) {
		return oldurl;
	}

	public void setOldurl( String oldurl ) {
		if( StringUtils.isNotBlank( oldurl )){
			oldurl = StringUtils.trim( decode( oldurl ) );
		}
		this.oldurl = oldurl;
	}

	public String getDescription( ) {
		return description;
	}

	public void setDescription( String description ) {
		if( StringUtils.isBlank( description )){
			description = name;
		}else{
			description = StringUtils.trim( decode( description ) );
		}
		this.description = description;
	}

	public Integer getIsdisplay( ) {
		return isdisplay;
	}

	public void setIsdisplay( Integer isdisplay ) {
		this.isdisplay = isdisplay;
	}

	public Integer getDisplayorder( ) {
		return displayorder;
	}

	public void setDisplayorder( Integer displayorder ) {
		if( displayorder == null ){
			displayorder = 0;
		}
		this.displayorder = displayorder;
	}
	
	
	
	
}
